---@class DIYUtility
local DIY = require "packages.diy_utility.diy_utility"

local skel = fk.CreateSkill {
  name = "rmt__zhengjiong",
  tags = {DIY.ReadySkill},
}

skel:addEffect(fk.BeforeTurnStart, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and
      data.reason == "game_rule"
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {skill_name =  skel.name, prompt = "#rmt__zhengjiong-invoke"})
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "@rmt__zhengjiong", 3)
    --- TODO: 感觉得改
    room:sendLog{
      type = "#GainAnExtraTurn",
      from = player.id
    }
    player.tag["_extra_turn_count"] = player.tag["_extra_turn_count"] or {}
    local ex_tag = player.tag["_extra_turn_count"]
    table.insert(ex_tag, skel.name)
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:getMark("@rmt__zhengjiong") > 0 then
      for _, move in ipairs(data) do
        if move.to == player and move.toArea == Card.PlayerHand then
          for _, info in ipairs(move.moveInfo) do
            if table.contains(player:getCardIds("h"), info.cardId) then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.to == player and move.toArea == Card.PlayerHand then
        for _, info in ipairs(move.moveInfo) do
          if table.contains(player:getCardIds("h"), info.cardId) then
            room:setCardMark(Fk:getCardById(info.cardId), "@@rmt__zhengjiong-inhand-turn", 1)
          end
        end
      end
    end
  end,
})

skel:addEffect(fk.TurnEnd, {
  late_refresh = true,
  can_refresh = function(self, event, target, player, data)
    if player:getMark("@rmt__zhengjiong") > 0 then
      return true
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:removePlayerMark(player, "@rmt__zhengjiong", 1)
    if player:getMark("@rmt__zhengjiong") > 0 then
      player:gainAnExtraTurn(true, skel.name)
      if player:getMark("@rmt__zhengjiong") == 2 then
        table.remove(player.tag["_extra_turn_count"])
      end
    end
  end,
})


skel:addLoseEffect(function (self, player, is_death)
  for _, id in ipairs(player:getCardIds("h")) do
    local card = Fk:getCardById(id)
    if card:getMark("@@rmt__zhengjiong-inhand-turn") ~= 0 then
      player.room:setPlayerMark(player, "@@rmt__zhengjiong-inhand-turn", 0)
    end
  end
end)

skel:addEffect("prohibit", {
  prohibit_use = function(self, from, card)
    if from:getMark("@rmt__zhengjiong") > 0 and card then
      if card:isVirtual() then
        if #card.subcards == 0 then
          return false
        else
          return table.find(card.subcards, function (id)
            return Fk:getCardById(id):getMark("@@rmt__zhengjiong-inhand-turn") == 0
          end)
        end
      else
        return card:getMark("@@rmt__zhengjiong-inhand-turn") == 0
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["rmt__zhengjiong"] = "争迥",
  [":rmt__zhengjiong"] = "<a href='diy_ready_skill'>蓄势技</a>，你的额定回合开始前，你可以改为执行三个额外回合，但这些回合中你只能使用本回合获得的牌。",

  ["#rmt__zhengjiong-invoke"] = "争迥：是否将你的额外回合改为执行3个额外回合？",
  ["@rmt__zhengjiong"] = "争迥",
  ["@@rmt__zhengjiong-inhand-turn"] = "争迥",
  ["#rmt__zhengjiong"] = "争迥",

  ["$rmt__zhengjiong1"] = "今日吾掌中之剑，必要饮兄弟之血！",
  ["$rmt__zhengjiong2"] = "外有强敌环伺，汝二人不欲助我、奈何助贼！",
}

return skel
